package com.shiwaixiangcun.customer.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.baidu.mobstat.SendStrategyEnum;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.shiwaixiangcun.customer.Common;
import com.shiwaixiangcun.customer.ConfigSps;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.base.BaseActivity;
import com.shiwaixiangcun.customer.entity.ResponseEntity;
import com.shiwaixiangcun.customer.network.NetworkHelper;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.JsonUtil;
import com.shiwaixiangcun.customer.utils.LoginOutUtil;
import com.shiwaixiangcun.customer.utils.RefreshTokenUtil;
import com.shiwaixiangcun.customer.utils.SharePreference;
import com.shiwaixiangcun.customer.widget.ChangeLightImageView;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;

/**
 * @author Administrator
 */
public class EditNameActivity extends BaseActivity implements View.OnClickListener {

    private ChangeLightImageView back_left;
    private TextView tv_page_name;
    private TextView tv_top_right;
    private EditText et_new_name;

    private NetworkHelper mNetworkHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_name);
        //        百度统计
        StatService.setLogSenderDelayed(10);
        StatService.setSendLogStrategy(this, SendStrategyEnum.APP_START, 1, false);
        StatService.setSessionTimeOut(30);

        mNetworkHelper = new NetworkHelper(mContext, getSupportFragmentManager());
        layoutView();
        initData();
    }

    private void layoutView() {
        back_left = findViewById(R.id.back_left);
        tv_page_name = findViewById(R.id.tv_page_name);
        tv_top_right = findViewById(R.id.tv_top_right);
        et_new_name = findViewById(R.id.et_new_name);
    }

    private void initData() {
        tv_page_name.setText("编辑姓名");
        tv_top_right.setVisibility(View.VISIBLE);
        tv_top_right.setText("保存");
        tv_top_right.setTextColor(Color.parseColor("#1CCC8C"));
        back_left.setOnClickListener(this);
        tv_top_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.back_left:
                finish();
                break;
            case R.id.tv_top_right:
                String str_name = this.et_new_name.getText().toString().trim();
                modifyUserName(str_name);
                break;
            default:
                break;
        }
    }

    private void modifyUserName(final String strName) {

        if (strName.isEmpty()) {
            showToastShort("请输入将要修改的姓名");
        } else {
            final String strRefreshToken = (String) AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_REFRESH_TOKEN, "");
            HttpParams httpParams = new HttpParams();
            httpParams.put("name", strName);
            mNetworkHelper.putRequestWithoutToken(GlobalApi.INSTANCE.getModify(), true, httpParams, new NetworkHelper.NetWorkHelperBack() {
                @Override
                public void requestSuccess(@NotNull String json) {
                    Type type = new TypeToken<ResponseEntity>() {
                    }.getType();
                    ResponseEntity responseEntity = JsonUtil.fromJson(json, ResponseEntity.class);

                    if (responseEntity.getResponseCode() == 1001) {
                        AppSharePreferenceMgr.put(mContext, ConfigSps.USER_NAME, strName);
                        SharePreference.saveStringToSpParams(EditNameActivity.this, Common.ISUSERNAME, Common.SIUSERNAME, et_new_name.getText().toString().trim());
                        Intent intent = new Intent();
                        intent.putExtra("newName", et_new_name.getText().toString().trim());
                        setResult(1717, intent);
                        finish();
                    } else if (responseEntity.getResponseCode() == 1018) {
                        RefreshTokenUtil.refreshToken(mContext, strRefreshToken);
                    } else if (responseEntity.getResponseCode() == 1019) {
                        LoginOutUtil.sendLoginOutUtil(mContext);
                    }

                }

                @Override
                public void requestFail() {

                }
            });


        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }
}
